/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-1-6 下午4:09 Created by lyyang
 */
package com.jade.bss.api.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO: Must write comments.
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-1-6 下午4:09
 */
@Controller
@RequestMapping (value="/shiro")
public class ShiroController
{

     protected Log logger = LogFactory.getLog(this.getClass());

    @RequestMapping (value="/login.do")
    @ResponseBody
    public void login(HttpServletRequest request,HttpSession session){
        logger.info("=====start test shiro login===");
        Subject user = SecurityUtils.getSubject();
        if (!user.isAuthenticated()){
            logger.info("=====user is not Authenticated,to login===");
            UsernamePasswordToken token = new UsernamePasswordToken("heben","passwd");
            user.login(token);

            logger.info("=====user isAuthenticated:" + user.isAuthenticated());
            logger.info("====shiro sessionid:=="+user.getSession().getId());
            session.setAttribute("jade_subject",user);


        }

    }

    @RequestMapping (value="/subject.do")
    @ResponseBody
    public void getSubject(Subject subject){
        logger.info("====subject==="+subject);
    }


    @RequestMapping (value="/session.do")
    @ResponseBody
    public void getSession(HttpServletRequest request){
        String sessionId = request.getParameter("sid");
        Subject subject = new Subject.Builder().sessionId(sessionId).buildSubject();
        logger.info("====subject==="+subject);
    }
}
